package com.example.milktea.dao.Impl;

import com.example.milktea.dao.ProductDao;
import com.example.milktea.entity.Product;
import com.example.milktea.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
@Repository
public class ProductDaoImpl implements ProductDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public List<Map<String, Object>> getProAll(){
        String sql = "select * from product";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    };
    public Product getProductById(Integer id) {
        String sql = "select * from product where product_id = ?";
        return this.jdbcTemplate.queryForObject(sql, new RowMapper<Product>() {

            @Override
            public Product mapRow(ResultSet rs, int rowNum) throws SQLException {
                Product product = new Product();
                product.setProductId(rs.getInt("product_id"));
                product.setProductName(rs.getString("product_name"));
                product.setProductDesc(rs.getString("product_descripe"));
                product.setProductSrc(rs.getString("product_src"));
                product.setProductPrice(rs.getString("product_price"));
                product.setProduct_store(rs.getString("product_store"));
                product.setProduct_profit(rs.getString("product_profit"));
                product.setProduct_status(rs.getInt("product_status"));
                return product;
            }
        }, id);
    }

    @Override
    public void modifyPrice(String product_id, Integer newPrice) {
        String sql = "update product set product_price = ? where product_id = ?";
        this.jdbcTemplate.update(sql,new Object[]{newPrice,product_id});
    }

    @Override
    public void updateStatus(String product_id, int status) {
        String sql = "update product set product_status = ? where product_id = ?";
        this.jdbcTemplate.update(sql,new Object[]{status,product_id});
    }

    public void updateProfit(String product_id,int product_profit) {
        String sql = "update product set product_profit = ? where product_id = ?";
        this.jdbcTemplate.update(sql,new Object[]{product_profit,product_id});
    }

    public int queryCost(int material_id){
        String sql = "select supplement_price from material where material_id = ?";
        return this.jdbcTemplate.queryForObject(sql,new Object[]{material_id},Integer.class);
    }

    @Override
    public void addProduct(Product product) {
        String sql = "insert into product(product_name,product_descripe,product_src,product_price,product_store,product_profit) values(?,?,?,?,?,?)";
        this.jdbcTemplate.update(sql,new Object[]{product.getProductName(),product.getProductDesc(),product.getProductSrc(),
                product.getProductPrice(),product.getProduct_store(),product.getProduct_profit()});
    }

    @Override
    public void onSale(String product_id) {
        String sql = "update product set product_status = 1 where product_id = ?";
        this.jdbcTemplate.update(sql,new Object[]{product_id});
    }

    @Override
    public void offSale(String product_id) {
        String sql = "update product set product_status = 0 where product_id = ?";
        this.jdbcTemplate.update(sql,new Object[]{product_id});
    }
}
